//
//  SetViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/21.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "SetViewController.h"
#import "MyCell.h"
#import "HBHLoginViewController.h"
#import "ChangePhoneViewController.h"
#import "ChangeMyMessageViewController.h"
#import "HBHTabBarViewController.h"

static NSString *const MyCellID = @"MyCellID";
@interface SetViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSArray *titleImageArr;//图片数组
@property (nonatomic,strong)UIButton *outBtn;//退出按钮

@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     [self initTitleWithTitle:@"设置"];
    // 初始化变量
    _titleImageArr = @[@[@"修改个人资料",@"xiugai_icon"],
                       @[@"修改手机号",@"tel02"],
                       @[@"清除缓存",@"clear_icon"]];
    [self.view addSubview:self.tableV];
    [self.view addSubview:self.outBtn];
}
-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64-60*screenW_6) style:UITableViewStyleGrouped];
        _tableV.rowHeight = 50*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
        [_tableV registerClass:[MyCell class] forCellReuseIdentifier:MyCellID];
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleImageArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50*screenW_6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //加载cell
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:MyCellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setTitleStr:_titleImageArr[indexPath.row][0]];
    [cell setHeaderStr:_titleImageArr[indexPath.row][1]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row ==  0) {//修改资料
        [self.navigationController pushViewController:[ChangeMyMessageViewController new] animated:YES];
    }else if (indexPath.row == 1){//修改手机号
        [self.navigationController pushViewController:[ChangePhoneViewController new] animated:YES];
    }else if (indexPath.row == 2){//清除缓存
        UIAlertController*alertController = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"确定清空缓存" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction*_Nonnullaction) {
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnullaction) {
            [HudHelper showProgressHudWithMessage:@"" toView:self.view];
            [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
                [HudHelper hideHudToView:self.view];
            }];
            [[SDImageCache sharedImageCache] clearMemory];
        }]];
        [self presentViewController:alertController animated:YES completion:^{
        }];
    }
}

-(UIButton *)outBtn//退出登陆按钮
{
    if (!_outBtn) {
        _outBtn = [TomamaUI createButton:UIButtonTypeCustom Frame:CGRectMake(15*screenW_6,SCREENH-64-60*screenW_6, SCREENW-30*screenW_6, 45*screenW_6) targ:self sel:@selector(outSelectBtn) titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:17*screenW_6] title:@"退出登陆" image:@"" backGroundImage:@""];
        _outBtn.backgroundColor = [UIColor colorWithHexString:@"FE8827"];
        [_outBtn.layer setCornerRadius:5*screenW_6];
        _outBtn.layer.masksToBounds = YES;
    }
    return _outBtn;
}
-(void)outSelectBtn//退出登陆
{
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//    [UINavigationBar appearance].barTintColor = [UIColor whiteColor];
//    [UINavigationBar appearance].translucent = NO;
    
    UIAlertController*alertController = [UIAlertController alertControllerWithTitle:@"确认退出登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction*_Nonnullaction) {
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnullaction) {
        appDelegate.window.rootViewController = [HBHTabBarViewController new];
        [Storage outLogin];
    }]];
    [self presentViewController:alertController animated:YES completion:^{
    }];
}
@end
